<?php

class GroupsController extends Controller {
    var $name = 'Groups';
    var $uses = array('Capabilities','Groups', 'Diagram', 'Sessions');
    var $components = array('group');
    var $session_key = array('group_id', 'post2category_id');

    function index() {
        $this->action = 'groups';
        $this->groups();
    }

    function groups($status = null) {
        $groups = $this->Groups->find_all('', array('id', 'title', 'is_anonymous', 'is_active'));
        $this->set('groups', $groups);
        if ($status == 'delete_failed') {
            $this->header_notice = __('Group delete faield.');
        } else if ($status == 'delete_done') {
            $this->header_notice = __('Group delete done.');
        }
    }

    function group_new($status = null) {
        $group_new = true;
        $diagrams = $this->Diagram->find_all("status='1'", array('id', 'type', 'title'), 'ORDER BY displayorder ASC');
        $this->set('diagrams', $diagrams);
        define('USE_SUB_MENU', true);
        $this->set('action_title', __('Create a new group'));
        $this->set('submit_value', __('Create new group'));

        if (!empty($this->data) && empty($status)) {
            if (empty($this->data['Groups']['title'])) {
                $this->header_notice = __('Group title is empty');
                return false;
            }
            if (empty($this->data['Groups']['id'])) {
                $this->data['Groups']['is_active'] = 1;
                $this->data['Groups']['is_anonymous'] = 0;
            } else {
                $this->Capabilities->group_id = $this->data['Groups']['id'];
                $group_new = false;
            }
            $this->Groups->save($this->data);
            if ($group_new) {
                $group_id = $this->Groups->get_last_insert_id();
            } else {
                $group_id = $this->data['Groups']['id'];
            }
            $category_cap = array();
            $page_cap = array();
            $admin = array();
            if (isset($this->data['Reading_category'])) {
                foreach($this->data['Reading_category'] as $reading) {
                    if (is_numeric($reading)) {
                        $reading_name = 'cap_read_' . $reading;
                        if (isset($this->data['Reading_category'][$reading_name])) {
                            $category_cap[$reading]['read'] = 1;
                        }
                    }
                }
            }

            if (isset($this->data['Reading_page'])) {
                foreach($this->data['Reading_page'] as $reading) {
                    if (is_numeric($reading)) {
                        $reading_name = 'cap_read_' . $reading;
                        if (isset($this->data['Reading_page'][$reading_name])) {
                            $page_cap[$reading]['read'] = 1;
                        }
                    }
                }
            }

            if (isset($this->data['Comment_category'])) {
                foreach($this->data['Comment_category'] as $comment) {
                    if (is_numeric($comment)) {
                        $comment_name = 'cap_comment_' . $comment;
                        if (isset($this->data['Comment_category'][$comment_name])) {
                            $category_cap[$comment]['comment'] = 1;
                        }
                    }
                }
            }

            if (isset($this->data['Comment_page'])) {
                foreach($this->data['Comment_page'] as $comment) {
                    if (is_numeric($comment)) {
                        $comment_name = 'cap_comment_' . $comment;
                        if (isset($this->data['Comment_page'][$comment_name])) {
                            $page_cap[$comment]['comment'] = 1;
                        }
                    }
                }
            }

            if (isset($this->data['Manage_category'])) {
                foreach($this->data['Manage_category'] as $manage) {
                    if (is_numeric($manage)) {
                        $manage_own_name = 'cap_manage_own_' . $manage;
                        $manage_all_name = 'cap_manage_all_' . $manage;
                        if (isset($this->data['Manage_category'][$manage_own_name])) {
                            $category_cap[$manage]['manage_own'] = 1;
                        }
                        if (isset($this->data['Manage_category'][$manage_all_name])) {
                            $category_cap[$manage]['manage_all'] = 1;
                        }
                    }
                }
            }
/*
            if (isset($this->data['Manage_page'])) {
                foreach($this->data['Manage_page'] as $manage) {
                    if (is_numeric($manage)) {
                        $manage_own_name = 'cap_manage_own_' . $manage;
                        $manage_all_name = 'cap_manage_all_' . $manage;
                        if (isset($this->data['Manage_page'][$manage_own_name])) {
                            $page_cap[$manage]['manage_own'] = 1;
                        }
                        if (isset($this->data['Manage_page'][$manage_all_name])) {
                            $page_cap[$manage]['manage_all'] = 1;
                        }
                    }
                }
            }
*/
            $Capabilities['Capabilities']['group_id'] = $group_id;
            $Capabilities['Capabilities']['key'] = 'Category';
            $Capabilities['Capabilities']['value'] = serialize($category_cap);
            if ($group_new) {
                $this->Capabilities->save($Capabilities);
            } else {
                $this->Capabilities->updateField($Capabilities['Capabilities']['key'], $Capabilities['Capabilities']['value']);
            }
            $Capabilities['Capabilities']['key'] = 'Page';
            $Capabilities['Capabilities']['value'] = serialize($page_cap);
            if ($group_new) {
                $this->Capabilities->save($Capabilities);
            } else {
                $this->Capabilities->updateField($Capabilities['Capabilities']['key'], $Capabilities['Capabilities']['value']);
            }
            $admin_menu = array();

            if (isset($this->data['Admin'])) {
                foreach($this->data['Admin'] as $key => $action) {
                    if ($action == 'none') {
                        continue;
                    }
                    $admin = array();
                    if ($action == 'read and manage') {
                        $admin['read'] = 1;
                        $admin['manage'] = 1;
                    } else if ($action == 'read') {
                        $admin['read'] = 1;
                    }

                    $admin_menu[$key] = $admin;

                }

                if (!empty($admin_menu['post_new']['manage'])) {
                    $admin_menu['drafts']['read'] = 1;
                    $admin_menu['drafts']['manage'] = 1;
                }
                $Capabilities['Capabilities']['value'] = serialize($admin_menu);
            } else {
                $Capabilities['Capabilities']['value'] = serialize(array());
            }
            $Capabilities['Capabilities']['key'] = 'AdminMenu';
            $Capabilities['Capabilities']['value'] = serialize($admin_menu);
            if ($group_new) {
                $this->Capabilities->save($Capabilities);
            } else {
                $this->Capabilities->updateField($Capabilities['Capabilities']['key'], $Capabilities['Capabilities']['value']);
            }

            if (!$group_new) {
                $redirect = '/groups/groups/edit_done';
            } else {
                $redirect = '/groups/groups/create_done';
            }
            $this->redirect($redirect);
            exit();
        }

    }

    function group_edit($group_id) {
        $this->action = 'group_new';
        $this->Groups->id = (int)$group_id;
        $group = $this->Groups->read();
        $capabilities = $this->Capabilities->find_all("group_id='$group_id'", array('`key`', '`value`'));
        foreach($capabilities as $capability) {
            $caps[$capability->key] = unserialize($capability->value);
        }
        if ($group->is_anonymous) {
            $this->set('is_anonymous', true);
        }
        $this->data['Groups']['id'] = $group_id;
        $this->data['Groups']['title'] = $group->title;
        $this->data['Groups']['description'] = $group->description;
        if (!empty($caps['Category'])) {
            foreach($caps['Category'] as $category_id => $action) {
                if (isset($action['read']) && $action['read'] == 1) {
                    $this->data['Reading_category']['cap_read_' . $category_id] = $category_id;
                }

                if (isset($action['comment']) && $action['comment'] == 1) {
                    $this->data['Comment_category']['cap_comment_' . $category_id] = $category_id;
                }

                if (isset($action['manage_own']) && $action['manage_own'] == 1) {
                    $this->data['Manage_category']['cap_manage_own_' . $category_id] = $category_id;
                }
                
                if (isset($action['manage_all']) && $action['manage_all'] == 1) {
                    $this->data['Manage_category']['cap_manage_all_' . $category_id] = $category_id;
                }
            }
            unset($caps['Category']);
        }

        if (!empty($caps['Page'])) {
            foreach($caps['Page'] as $page_id => $action) {
                if (isset($action['read']) && $action['read'] == 1) {
                    $this->data['Reading_page']['cap_read_' . $page_id] = $page_id;
                }
                if (isset($action['comment']) && $action['comment'] == 1) {
                    $this->data['Comment_page']['cap_comment_' . $page_id] = $page_id;
                }
            }
            unset($caps['Page']);
        }
        
        if (!empty($caps['AdminMenu'])) {
            foreach($caps['AdminMenu'] as $key => $action) {
                if (isset($action['manage']) && $action['manage'] ==1) {
                    $this->data['Admin'][$key] = 'read and manage';
                } else if (isset($action['read']) && $action['read'] ==1) {
                    $this->data['Admin'][$key] = 'read';
                } else {
                    $this->data['Admin'][$key] = 'none';
                }
            }
        }
        $this->group_new('edit');
        $this->set('action_title', __('Edit group'));
        $this->set('submit_value', __('Update group'));
    }

    function delete_group($group_id) {
        $this->layout = null;
        $redirect = '/groups/groups/delete_failed/';
        $group_id = (int)$group_id;
        if (!empty($group_id)) {
            $user_count = get_group_user_count($group_id);
            if (empty($user_count)) {
                $this->Groups->del($group_id);
                $redirect = '/groups/groups/delete_done/';
            }
        }
        $this->redirect($redirect);
    }
}

?>
